package com.saturday.common.jwt;

import com.saturday.common.util.JwtTokenUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class JwtTokenComponent {

    @Value("${jwt.secret:}")
    private String secret;

    @Value("${access.token:access-token}")
    private String tokenName;

    /**
     * jwt 实际生成token
     *
     * @param sessionId sessionId
     * @param username  用户登陆名
     * @param expire    过期时间
     * @return
     */
    public String generateToken(String sessionId, String username, Date issueAt, long expire) {
        Map<String, Object> userDetails = new HashMap<>();
        userDetails.put("username", username);
        return JwtTokenUtils.generateToken(sessionId, userDetails, issueAt, expire, this.secret);
    }

    /**
     * 解析sessionId
     *
     * @param token
     * @return
     */
    public String getSessionIdFromToken(String token) {
        return JwtTokenUtils.getSessionIdFromToken(token, this.secret);
    }

}
